package com.zetyun.tiger.format.date.epay;

import com.zetyun.rt.meta.annotation.ActionMeta;
import com.zetyun.rt.sdk.action.MapAction;
import com.zetyun.rt.sdk.model.RtEvent;
import com.zetyun.rt.sdk.operator.OperatorContext;
import com.zetyun.tiger.format.date.SpecialDateFormatUtil;
import com.zetyun.tiger.format.date.ebuspboracle.EBUSPBORACLEDateFormatParameter;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

import static com.zetyun.tiger.format.date.FormatConstant.*;

//@ActionMeta(
//        id = "tiger.epay-format-date",
//        tags = {"formatter"},
//        category = "map",
//        name = "EPAY-DateFormat",
//        parameterClass = EpayDateFormatParameter.class,
//        status = ActionMeta.Status.PUBLIC,
//        description = "Epay 系统表统一日期格式"
//)
@Slf4j
public class EpayDateFormatAction extends MapAction {


    private String epayDateField;


    @Override
    public void init(OperatorContext context) {
        super.init(context);
        EpayDateFormatParameter parameters = context.getParameters();
        epayDateField = parameters.getEpayDateField().trim();

    }

    @Override
    public RtEvent apply(RtEvent in) throws Exception {

        long procTs = System.currentTimeMillis();
        String currentSystemDate = SpecialDateFormatUtil.toYmdHms(procTs);
        in.setField(RT_PROC_TMS, procTs);
        in.setField(RT_PROC_TIME, currentSystemDate);

        Object dataObject = in.getField(epayDateField);
        if (Objects.nonNull(dataObject) && !dataObject.toString().isEmpty()) {
            Long dataTs = SpecialDateFormatUtil.toEpayTs(dataObject.toString());
            String formatDate = SpecialDateFormatUtil.toYmdHms(dataTs);
            in.setField(RT_EVENT_TMS, dataTs);
            in.setField(RT_EVENT_TIME, formatDate);
            in.setField(RT_EVENT_DATE, SpecialDateFormatUtil.day(formatDate));
            in.setField(RT_EVENT_HOUR, SpecialDateFormatUtil.hour(formatDate));
            in.setField(RT_EVENT_MINU, SpecialDateFormatUtil.minute(formatDate));
        } else {
            in.setField(RT_EVENT_TMS, procTs);
            in.setField(RT_EVENT_TIME, currentSystemDate);
            in.setField(RT_EVENT_DATE, SpecialDateFormatUtil.day(currentSystemDate));
            in.setField(RT_EVENT_HOUR, SpecialDateFormatUtil.hour(currentSystemDate));
            in.setField(RT_EVENT_MINU, SpecialDateFormatUtil.minute(currentSystemDate));
        }

        in.setField(RT_EVENT_TYPE, in.getField("op_"));
        in.removeField("op_");
        return in;
    }
}
